﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using GBIT.Web;
using WebComponents.Data;
using WebComponents;
using System.Collections.Generic;

public partial class Admincp_MasterPage : System.Web.UI.MasterPage {
  public string strURL = "default.aspx?tabid={0}&module={1}";
  int tabid = 0;
  public string user = "";
  protected void Page_Load(object sender, EventArgs e) {
    if (Session["login"] == null)
      Response.Redirect("Login.aspx");
    else {
      user = (Session["login"] as UserInfo).UserName.ToLower();
    }
    Response.Cookies[Session.SessionID].Value = "True";
    Response.Cookies[Session.SessionID].Expires = DateTime.Now.AddHours(3);
    List<MenuInfo> menuLevel1 = MenuController.GetMenuLevel1(StatusType.IsActive);
    if ((Session["login"] as UserInfo).IsApproved == false)
      menuLevel1 = menuLevel1.Where(m => m.MenuID != 13 && m.MenuID != 14 && m.MenuID != 15).ToList();
    rptTabMenuLevel1.DataSource = menuLevel1;
    rptTabMenuLevel1.DataBind();
    tabid = ConvertType.ToInt(Request.Params["tabid"]);
    if (tabid == 0) {
      if (menuLevel1.Count > 0)
        tabid = menuLevel1[0].MenuID;
    }
    if (Session["CurrentControl"] == null) {
      topToolBar.Visible = false;
      bottomToolBar.Visible = false;
    } else {
      UserControlBase u = (UserControlBase)Session["CurrentControl"];
      u.Load += new EventHandler(u_Load);
    }

  }
  void u_Load(object sender, EventArgs e) {
    UserControlBase u = (UserControlBase)sender;

    topToolBar.Visible = u.ToolBarVisible;
    bottomToolBar.Visible = false; //u.ToolBarVisible;
    this.Page.Title = u.Title;
    if (u.TaskTitle != "")
      lblTitle.Text = u.Title + ": [" + u.TaskTitle + "]";
    else
      lblTitle.Text = u.Title;
    btnAdd.Enabled = btnAddBottom.Enabled = u.AddEnabled;
    btnAdd.Visible = btnAddBottom.Visible = u.AddVisible;

    btnSave.Enabled = btnSaveBottom.Enabled = u.SaveEnabled;
    btnSave.Visible = btnSaveBottom.Visible = u.SaveVisible;

    btnEdit.Enabled = btnEditBottom.Enabled = u.EditEnabled;
    btnEdit.Visible = btnEditBottom.Visible = u.EditVisible;

    btnDelete.Enabled = btnDeleteBottom.Enabled = u.DeleteEnabled;
    btnDelete.Visible = btnDeleteBottom.Visible = u.DeleteVisible;

    btnApply.Enabled = btnApplyBottom.Enabled = u.DeleteEnabled;
    btnApply.Visible = btnApplyBottom.Visible = u.DeleteVisible;

    btnCancel.Enabled = btnCancelBottom.Enabled = u.CancelEnabled;
    btnCancel.Visible = btnCancelBottom.Visible = u.CancelVisible;

    btnDelete.Attributes.Add("onclick", string.Format("return ConfirmDelete('{0}${1}',this)", this.ClientID, "btnDelete"));
    btnDeleteBottom.Attributes.Add("onclick", string.Format("return ConfirmDelete('{0}${1}',this)", this.ClientID, "btnDeleteBottom"));

    if (u.EditEnabled) {
      btnEdit.Attributes.Add("onclick", string.Format("return ConfirmEdit('{0}${1}',this)", this.ClientID, "btnEdit"));
      btnEditBottom.Attributes.Add("onclick", string.Format("return ConfirmEdit('{0}${1}',this)", this.ClientID, "btnEditBottom"));
    }
    if (u.AddTitle == "")
      u.AddTitle = "Thêm mới";
    if (u.SaveTitle == "")
      u.SaveTitle = "Lưu lại";
    if (u.DeleteTitle == "")
      u.DeleteTitle = "Xóa";
    if (u.EditTitle == "")
      u.EditTitle = "Sửa đổi";
    if (u.ApplyTitle == "")
      u.ApplyTitle = "Áp dụng";
    if (u.CancelTitle == "")
      u.CancelTitle = "Hủy bỏ";

    if (u.AddEnabled)
      btnAdd.Text = btnAddBottom.Text = "<span class='item-new'></span>" + u.AddTitle;
    else
      btnAdd.Text = btnAddBottom.Text = "<span class='item-new-disable'></span>" + u.AddTitle;
    if (u.SaveEnabled)
      btnSave.Text = btnSaveBottom.Text = "<span class='item-save'></span>" + u.SaveTitle;
    else
      btnSave.Text = btnSaveBottom.Text = "<span class='item-save-disable'></span>" + u.SaveTitle;
    if (u.EditEnabled)
      btnEdit.Text = btnEditBottom.Text = "<span class='item-edit'></span>" + u.EditTitle;
    else
      btnEdit.Text = btnEditBottom.Text = "<span class='item-edit-disable'></span>" + u.EditTitle;
    if (u.DeleteEnabled)
      btnDelete.Text = btnDeleteBottom.Text = "<span class='item-delete'></span>" + u.DeleteTitle;
    else
      btnDelete.Text = btnDeleteBottom.Text = "<span class='item-delete-disable'></span>" + u.DeleteTitle;
    if (u.ApplyEnabled)
      btnApply.Text = btnApplyBottom.Text = "<span class='item-apply'></span>" + u.ApplyTitle;
    else
      btnApply.Text = btnApplyBottom.Text = "<span class='item-apply-disable'></span>" + u.ApplyTitle;
    if (u.CancelEnabled)
      btnCancel.Text = btnCancelBottom.Text = "<span class='item-cancel'></span>" + u.CancelTitle;
    else
      btnCancel.Text = btnCancelBottom.Text = "<span class='item-cancel-disable'></span>" + u.CancelTitle;

    if (u.AddModule != "") {
      //MenuInfo menu = MenuController.GetByURL(u.AddModule);
      //if (menu != null) {
      string encode = ConvertType.Encode(u.AddModule);
      btnAdd.Attributes.Add("onclick", string.Format("javascript:window.location='?tabid={0}&mod={1}'; return false;", tabid, encode));
      btnAddBottom.Attributes.Add("onclick", string.Format("javascript:window.location='?tabid={0}&mod={1}'; return false;", tabid, encode));
      //}
    }
    //strURL = string.Format(strURL, tabid, u.ReleaseModule);
    if (IsPostBack) {
      string task = ConvertType.ToString(Request.Params["task"]).Trim();
      object objID = Request.Params["taskid"];
      if (task != "") {
        if (u.TaskSubmit != null) {
          TaskArgs arg = new TaskArgs();
          arg.ID = objID;
          switch (task.ToLower()) {
            case "moveup":
              arg.Task = Task.MoveUp;
              break;
            case "movedown":
              arg.Task = Task.MoveDown;
              break;
            case "saveorder":
              arg.Task = Task.SaveOrder;
              break;
            case "changestatus":
              arg.Task = Task.ChangeStatus;
              break;
          }
          u.TaskSubmit(sender, arg);
        }
      }
    }
  }

  protected void rptTabMenuLevel1_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    MenuInfo menu = e.Item.DataItem as MenuInfo;
    if (menu != null) {
      MenuDescriptionInfo menuDescription = MenuDescriptionController.GetById(menu.MenuID, "vi-VN");
      HyperLink hpLink = e.Item.FindControl("hpLinkItem") as HyperLink;
      if (menu.MenuUrl.EndsWith(".ascx"))
        hpLink.NavigateUrl = string.Format("default.aspx?tabid={0}&cid={1}", menu.ParentID, menu.MenuID);
      else
        hpLink.NavigateUrl = menu.MenuUrl;
      if (menuDescription != null) {
        hpLink.Text = menuDescription.Title + "&nbsp;";
      }
      List<MenuInfo> list = MenuController.GetByParentId(menu.MenuID, StatusType.IsActive);
      Repeater rptTabMenuItem = e.Item.FindControl("rptTabMenuItem") as Repeater;
      rptTabMenuItem.ItemDataBound += new RepeaterItemEventHandler(rptTabMenuItem_ItemDataBound);
      rptTabMenuItem.DataSource = list;
      rptTabMenuItem.DataBind();
    }
  }
  void rptTabMenuItem_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    MenuInfo menu = e.Item.DataItem as MenuInfo;
    if (menu != null) {
      MenuDescriptionInfo menuDescription = MenuDescriptionController.GetById(menu.MenuID, "vi-VN");
      HyperLink hpLink = e.Item.FindControl("hpLinkItem") as HyperLink;
      if (menu.MenuUrl.EndsWith(".ascx"))
        hpLink.NavigateUrl = string.Format("default.aspx?tabid={0}&cid={1}", menu.ParentID, menu.MenuID);
      else
        hpLink.NavigateUrl = menu.MenuUrl;
      if (menuDescription != null) {
        hpLink.Text = string.Format("<span class='SubMenuText'><strong>{0}</strong> <span>{1}</span> </span>", menuDescription.Title, menuDescription.Description);
        //hpLink.CssClass = menuDescription.ClassName;
      }
    }
  }
  protected void btnAdd_Click(object sender, EventArgs e) {

  }
  protected void btnSave_Click(object sender, EventArgs e) {
    if (Session["CurrentControl"] != null) {
      UserControlBase u = (UserControlBase)Session["CurrentControl"];
      if (u.SaveRecord != null)
        u.SaveRecord(sender, e);
    }
  }
  protected void btnDelete_Click(object sender, EventArgs e) {
    if (Session["CurrentControl"] != null) {
      UserControlBase u = (UserControlBase)Session["CurrentControl"];
      if (u.DeleteRecord != null)
        u.DeleteRecord(sender, e);
    }
  }
  protected void btnEdit_Click(object sender, EventArgs e) {
    if (Session["CurrentControl"] != null) {
      UserControlBase u = (UserControlBase)Session["CurrentControl"];
      if (u.EditRecord != null)
        u.EditRecord(sender, e);
    }
  }
  protected void btnApply_Click(object sender, EventArgs e) {
    if (Session["CurrentControl"] != null) {
      UserControlBase u = (UserControlBase)Session["CurrentControl"];
      if (u.Apply != null)
        u.Apply(sender, e);
    }
  }
}
